﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FSky;
using System.Windows.Forms; // 确保引用了System.Windows.Forms命名空间
namespace idea0
{
    public class Class2_AI : Class2
    {
        public DateTime LastMoveTime = DateTime.MinValue;
        public static readonly int MOVE_COOLDOWN = 500; // 500 milliseconds
        public System.Drawing.Image AIImage { get; set; }
        public int MaxLife { get; set; } = 1;
        public Class2_AI(Class1 blackWalls, World world) : base(blackWalls, world)
        {
            // 初始化AI相关属性
            string AIPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "gif", "a5.gif");
            AIImage = System.Drawing.Image.FromFile(AIPath);
            Life = 1;
        }

        public void Update(World world)
        {
            if (DateTime.Now - LastMoveTime > TimeSpan.FromMilliseconds(MOVE_COOLDOWN))
            {
                // AI移动逻辑
                Random rand = new Random();
                char[] directions = { 'w', 's', 'a', 'd' };
                char move = directions[rand.Next(directions.Length)];
                KeyPress(move);
                LastMoveTime = DateTime.Now;
            }

            // AI捡道具逻辑
            int gridX = GridPosition.Item1;
            int gridY = GridPosition.Item2;
            if (world.WhitePieces.IsWhiteCell(gridX, gridY))
            {
                // 捡起道具，增加生命等效果
                world.WhitePieces.WhiteBoard[gridY, gridX] = 0;
                Life++;
            }

            // AI放置炸弹逻辑
            PlaceBomb(world);
        }
        public void Draw(Graphics g)
        {
            g.DrawImage(AIImage, X - W / 2, Y - H / 2, W, H);
        }
        public void PlaceBomb(World world)
        {
            int x = GridPosition.Item1;
            int y = GridPosition.Item2;

            if (world.IsBombPresent(x, y))
            {
                return;
            }

            // 假设AI有BombPlusCount和LastBombTime属性
            if (BombPlusCount > 0)
            {
                Bomb bomb = new Bomb(x, y);
                bomb.BombOwner = this; // 设置炸弹所有者
                Bombs.Add(bomb);
                world.Bombs.Add(bomb);
                BombPlusCount--;
            }
            else if (DateTime.Now - LastBombTime >= TimeSpan.FromSeconds(3))
            {
                Bomb bomb = new Bomb(x, y);
                bomb.BombOwner = this; // 设置炸弹所有者
                Bombs.Add(bomb);
                world.Bombs.Add(bomb);
                LastBombTime = DateTime.Now;
            }
        }
    }
}